﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;
using VehicleMaintenance.Events;

namespace VehicleMaintenance.BaseInfo
{
	public partial class GoodListForm : ListBaseForm
	{
		public GoodListForm()
		{
			InitializeComponent();
		}

		public GoodListForm(bool select)
		{
			InitializeComponent();
			IsSelect = select;
		}
		private Goods good;

		public Goods Good
		{
			get { return good; }
			set { good = value; }
		}

		private void CarListForm_Load(object sender, EventArgs e)
		{
			try
			{
				cboxType.SelectedIndex = 0;
				BindData();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			try
			{
				GoodsForm form = new GoodsForm();
				form.ShowInTaskbar = false;
				form.ShowDialog(this);

				BindData();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			try
			{
				if (lvData.SelectedItems.Count > 0)
				{
					GoodsForm form = new GoodsForm(Convert.ToInt32(lvData.SelectedItems[0].Tag));
					form.ShowInTaskbar = false;
					form.ShowDialog(this);

					BindData();
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}


		private void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
				if (lvData.SelectedItems.Count > 0)
				{
					if (MessageBox.Show(this, "是否删除当前车辆信息？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
					{
						CarDBContext db = new CarDBContext();

						int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
						db.Goods.Remove(db.Goods.First(p => p.GoodID == id));

						db.SaveChanges();

						BindData();
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void BindData()
		{
			try
			{
				lvData.Items.Clear();

				CarDBContext db = new CarDBContext();
				var list = db.Goods.Include("CarType").Include("GoodType").OrderBy(p => p.GoodID);

				switch (cboxType.Text)
				{
					case "商品类型":
						{
							int id = Convert.ToInt32(txtValue.Tag);
							list = db.Goods.Include("CarType").Include("GoodType").Where(p => p.GoodTypeID == id).OrderBy(p => p.GoodID);
							break;
						}
					case "车型":
						{
							int id = Convert.ToInt32(txtValue.Tag);
							list = db.Goods.Include("CarType").Include("GoodType").Where(p => p.CarTypeID == id).OrderBy(p => p.GoodID);
							break;
						}
					case "商品编码":
						{
							list = db.Goods.Include("CarType").Include("GoodType").Where(p => p.Code.IndexOf(txtValue.Text.Trim()) > -1).OrderBy(p => p.GoodID);
							break;
						}
					case "商品名称":
						{
							list = db.Goods.Include("CarType").Include("GoodType").Where(p => p.Name.IndexOf(txtValue.Text.Trim()) > -1).OrderBy(p => p.GoodID);
							break;
						}
					default:
						{
							break;
						}
				}

				foreach (var item in list)
				{
					ListViewItem lvItem = new ListViewItem(new string[] { item.GoodType.Name,item.Code,  item.Name,item.CarType.Name, item.Units, item.Placeoforigin, 
                                item.PurchasePrice.ToString(),item.WholesalePrice.ToString(),item.PurchasePrice.ToString(), 
                                item.Producer,item.Number.ToString(),item.Top.ToString(),item.Bottom.ToString(),item.Remark});
					lvItem.Tag = item.GoodID;
					lvData.Items.Add(lvItem);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private void lvData_DoubleClick(object sender, EventArgs e)
		{
			if (lvData.SelectedItems.Count > 0)
			{
				if (IsSelect)
				{
					CarDBContext db = new CarDBContext();

					int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
					good = db.Goods.First(p => p.GoodID == id);

					this.DialogResult = System.Windows.Forms.DialogResult.OK;
				}
				else
				{
					btnEdit_Click(sender, e);
				}
			}
		}

		private void btnQuery_Click(object sender, EventArgs e)
		{
			try
			{
				if (cboxType.SelectedIndex > 0)
				{
					if (txtValue.Text.Trim().Length == 0)
					{
						this.ShowInfo("请输入查询条件值");
						return;
					}
				}
				BindData();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void cboxType_SelectedIndexChanged(object sender, EventArgs e)
		{
			txtValue.Enabled = true;
			txtValue.Clear();
			txtValue.Click -= txtTypeValue_Click;
			txtValue.Click -= txtCarValue_Click;
			txtValue.KeyPress -= KeyEvents.DisabledInput_KeyPress;
			txtValue.KeyDown -= KeyEvents.DisabledInput_KeyDown;
			switch (cboxType.Text)
			{
				case "商品类型":
					{

						txtValue.Click += new EventHandler(txtTypeValue_Click);
						txtValue.KeyPress += KeyEvents.DisabledInput_KeyPress;
						txtValue.KeyDown += KeyEvents.DisabledInput_KeyDown;
						break;
					}
				case "车型":
					{

						txtValue.Click += new EventHandler(txtCarValue_Click);
						txtValue.KeyPress += KeyEvents.DisabledInput_KeyPress;
						txtValue.KeyDown += KeyEvents.DisabledInput_KeyDown;
						break;
					}
				case "商品编码":
					{
						txtValue.Focus();
						break;
					}
				case "商品名称":
					{
						txtValue.Focus();
						break;
					}
				default:
					{
						txtValue.Enabled = false;
						break;
					}
			}
		}

		void txtTypeValue_Click(object sender, EventArgs e)
		{
			try
			{

				TextBox txt = (sender as TextBox);

				GoodTypeForm form = new GoodTypeForm();
				if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					txt.Text = form.GoodType.Name;
					txt.Tag = form.GoodType.GoodTypeID;
				}

			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}
		void txtCarValue_Click(object sender, EventArgs e)
		{
			try
			{

				TextBox txt = (sender as TextBox);

				CarTypeForm form = new CarTypeForm();
				if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					txt.Text = form.CarType.Name;
					txt.Tag = form.CarType.CarTypeID;
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}
	}
}
